package com.docker.controller;

import com.docker.dto.UserDTO;
import com.docker.dto.UserLoginDTO;
import com.docker.dto.UserUpdateDTO;
import com.docker.entity.Result;
import com.docker.entity.User;
import com.docker.properties.JwtProperties;
import com.docker.service.UserService;
import com.docker.utils.JwtUtil;
import com.docker.vo.UserLoginVO;
import com.docker.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    @PostMapping ("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info(userLoginDTO.toString());
        // TODO 登录逻辑
        User user = userService.login(userLoginDTO);
        // 登录成功后返回token
        UserLoginVO userLoginVO = new UserLoginVO();
        BeanUtils.copyProperties(user, userLoginVO);

        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);
        // 这里返回token，实际项目中应该返回json数据，包含token信息
        userLoginVO.setToken(token);
        return Result.success(userLoginVO);
    }


    /**
     * 获取所有用户信息
     * @return
     */
    @GetMapping("/getUsers")
    public Result<List<User>> getUsers(@RequestParam(value = "page" ,defaultValue = "1") Integer page,@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        // TODO 获取用户信息
        List<User> User = userService.getUsers(page,pageSize);
        return Result.success(User);
    }

    /**
     * 更新用户信息
     * @param userUpdateDTO
     * @return
     */
    @PutMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody UserUpdateDTO userUpdateDTO) {
        log.info("update user info: " + userUpdateDTO.toString());
        // TODO 更新用户信息
        userService.updateUserInfo(userUpdateDTO);

        return Result.success();
    }

    /**
     * 添加用户信息
     * @param userDTO
     * @return
     */
    @PostMapping("/addUser")
    public Result<UserVo> addUser(@RequestBody UserDTO userDTO){
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        userService.addUser(user);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(userDTO,userVo);
        return Result.success(userVo);
    }

    @GetMapping("/getUserByParam")
    public Result<List<UserVo>> getUserByParam(@RequestParam(value = "name" ,defaultValue = "姚佳男")String name){
        List<UserVo> userVoList = new ArrayList<>();
        List<User> userList = userService.getUserByParam(name);
        if (userList==null)
            return Result.error("未查询到此人");
        for (User user:userList){
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user,userVo);
            userVoList.add(userVo);
        }

        return Result.success(userVoList);
    }

    @DeleteMapping("/delUserById")
    public Result delUserByStudentId(@RequestParam(value = "Id" ,defaultValue = "")Integer Id){
        userService.delUserById(Id);
        return Result.success();
    }
}
